import { Component, OnInit, ViewChild, ViewContainerRef, ComponentRef, ComponentFactoryResolver, EventEmitter } from '@angular/core';
import { TableService } from "./table.service";
import { InvestService } from "../../invest.service";
import { DataService } from 'app/services/data.service';
import { Util } from 'app/common/util';
import * as _ from 'lodash';

@Component({
  selector: 'app-table',
  templateUrl: './table.component.html',
  styleUrls: ['./table.component.scss'],
  providers: [TableService]
})
export class TableComponent implements OnInit {
  // lodash
  public _: any = _;
  // 字典值
  public dictionaryList: any = {
    ID_FIELD_TYPE: []
  };
  // 列表新数据
  public dataList: any = [];
  // 克隆列表新数据,用于页面保存、提交时对比判断是否有修改
  public cloneList: any = [];
  // 克隆列表新数据,用于页面数据标红对比
  public isChangeList: any = [];
  // 用于控制下拉框显示
  public isEditList: any = [];
  // 列表是否全选
  public isCheckedAll = false;
  // 当前登录人
  public user: any;
  // 第一列显示的字段
  public isShowCol1: any = '';
  // 第二列显示的字段
  public isShowCol2: any = '';
  // 第三列显示的字段
  public isShowCol3: any = '';
  // 是否为复核页面引用table,用于控制复选框显示
  public isReviewPage: any = 'no';
  // 是否可以双击进入modal,用于控制双击事件
  public isEdit: any = 'no';

  constructor(
    public tableService: TableService,
    public resolver: ComponentFactoryResolver,
    public investService: InvestService,
    public dataService: DataService,
  ) { }

  ngOnInit() {
    // 获取当前用户
    this.user = sessionStorage.getItem('username');
    // 字典值
    this.dictionaryList = JSON.parse(localStorage.getItem("dictionaryList"));
  }
  // 列表全选(所有列表公用)
  checkedAll() {
    if (this.isCheckedAll) {
      _.forEach(this.dataList, item => {
        item.isChecked = true;
      });
    } else {
      _.forEach(this.dataList, item => {
        item.isChecked = false;
      });
    }
  }
  // 判断列表是否全选
  ctrlCheckAll() {
    let that = this;
    let checkList = [];
    let operateItem = [];
    switch (this.isReviewPage) {
      case 'no':
        _.forEach(this.dataList, item => {
          if (item.isChecked) {
            checkList.push(item);
          }
        })
        if (checkList.length == this.dataList.length) {
          this.isCheckedAll = true;
        } else {
          this.isCheckedAll = false;
        }
        break;
      case 'yes':
        _.forEach(this.dataList, item => {
          if (item.isChecked && item.sOprState == '1') {
            checkList.push(item);
          }
          if (item.sOprState == '1') {
            operateItem.push(item);
          }
        })
        if (checkList.length == operateItem.length) {
          this.isCheckedAll = true;
        } else {
          this.isCheckedAll = false;
        }
        break;
    }
  }
  // 显示下拉框(index为下标,name为字段名,sStepZh为当前指标状态转义中文,sOpr为操作人)
  showSelect(index: any, name: any, sStepZh: any, sOpr: any) {
    let that = this;
    this.user = sessionStorage.getItem('username');
    console.info(this.user);
    
    if (this.isEdit == 'yes') {
      if (sStepZh == '正常') {
        that.isEditList[index][name] = 'yes';
      } else {
        // 先判断数据的操作人是否为当前,再判断指标状态
        if (sOpr == this.user || sOpr == null || sOpr == '') {
          if (sStepZh == '风险管理部复核' || sStepZh == '指标复核') {
            window['swal']("提示", "指标状态为 " + sStepZh + " 不可修改", "warning");
          } else {
            that.isEditList[index][name] = 'yes';
          }
        } else {
          window['swal']("提示", "不能修改 " + sOpr + " 修改过的数据", "warning");
        }
      }
    }
  }
  // 修改标蓝(index为下标,value为修改后的值,name为字段名,sStepZh为当前指标状态转义中文,sOpr为操作人)
  inputChange(index: any, value: any, name: any) {
    let that = this;
    // 定位tr
    let i = index + 1;
    // 定位输入框
    let id = "#modify-table tbody tr:nth-child(" + i + ") td div[id=" + name + "]";
    // 获取当前修改对象
    let item = this.cloneList[index];
    // 获取原数据
    let _value = _.filter(item, function (val, key) {
      if (key == name) {
        return val
      }
    })
    // 对比标蓝
    if (_value == value) {
      window['$'](id).removeClass('triangle-topleft');
    } else {
      window['$'](id).addClass('triangle-topleft');
    }
    this.isEditList[index][name] = 'no';
  }
}
